import axios from "@/axios"
import store from "@/store"
const keyCode = 'KSWBZ-H5U64-LOQUQ-DTTZD-4S3E2-ZQF6D'

async function getLocalLocation(){
   return new Promise((resolve ,reject) =>{
   try { 
     window.navigator.geolocation.getCurrentPosition(function (position){
        let  res =  position.coords.latitude + ',' + position.coords.longitude
        resolve(res)
     })
   } catch (error) {
      return resolve(null)
   }
  })
}
export const location =  {

  async getlocation(){
     try{
        let res = await axios({ method: 'GET', url: `/tx/ws/location/v1/ip?key=${keyCode}`,  })   
         if(res.data.message=='Success'){
            let address=res.data.result.ad_info.city +" " +res.data.result.ad_info.district
            let ip=res.data.result.ip
            let data={address,ip,location:res.data.result.location}
             
            // let nowLocation = await getLocalLocation()  || res.data.result.location.lat + ',' + res.data.result.location.lng

            // let result = await axios({ method: 'GET', url: `/tx/ws/geocoder/v1/?location=${nowLocation}&key=${keyCode}`  })  
      
      
            store.commit('Init/SETLOCATION',data)
            store.commit('Init/SETCOMPUTERINFO',{ip,loginAddress:address})
          }
         return {code:res.status,messgae:res.data.message}
     }catch(error) { return {code:500,messgae:error}}
    }
}